/*
 * HelloMidlet.java
 *
 * Created on July 16, 2007, 4:33 PM
 */

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.io.InputStream;

import javax.microedition.media.*;
import javax.microedition.media.control.*; 
import java.util.*;

import javax.microedition.io.file.*;
import javax.microedition.pim.*;
import javax.microedition.io.*;

/**
 *
 * @author iftee
 */
public class HelloMidlet extends MIDlet implements CommandListener {
    
    /** Creates a new instance of HelloMidlet */
    
    Player player;
    boolean uploading = false;    
    
    Command uploadVideoCommand=new Command("Upload", Command.ITEM, 1);
    Command stopCapture=new Command("Stop", Command.ITEM, 1);
    Command startCapture = new Command("Start Capture", Command.ITEM, 1);
    Command uploadOKCommand = new Command("Upload Now", Command.OK, 1);
    
    
    Form menuScreen=new Form("nothing");
    
    
    
    VideoControl videoControl;
    String serverIP, serverPort, serverAddress;
    
    boolean playBack = false;
    boolean preView = false;
    
    ByteArrayOutputStream baos;
    
    
    public HelloMidlet() {
          
    }
    
    private Command exitCommand;//GEN-LINE:MVDFields
    
    public void initPlayer()
    {
     try{
            player = Manager.createPlayer("capture://video"); 
        
     }catch(Exception ex){
         
         try{
               
               player = Manager.createPlayer("capture://audio_video");

         }catch( Exception e){
                 
             showException("Error", new Exception("Cannot Capture"), null);
             
         }
     }
    }
    
    
    public void showException(String sDescription, Exception e, final Displayable disp) {        
        //TODO try write the log entry to socket                    
        
        String sMessage = "";
        if (sDescription != null) {
            sMessage += sDescription;
        }
        if ((sDescription != null) && (e != null)) {
            sMessage += "\n\n";
        }
        if (e != null) {
            
            sMessage += e.getMessage();
        }
        
        //alert the user
        Alert alert = null;
        if( sDescription.startsWith("Notification"))
            alert= new Alert("", sMessage, null, AlertType.WARNING);
        else 
            if( sDescription.startsWith("Error"))
             alert= new Alert("", sMessage, null, AlertType.ERROR);        

        alert.setTimeout(Alert.FOREVER);
        alert.setCommandListener(new CommandListener() {
            public void commandAction(Command command, Displayable displayable) {
                
                if (command == Alert.DISMISS_COMMAND) {
                    //exit the app
                    if (disp == null) 
                    {            //stop the app when alert is dismissed
                        destroyApp(false);
                        notifyDestroyed();
                    }
                    else
                    {
                        getDisplay().setCurrent(disp);
                    }
                }
            }
        });

        getDisplay().setCurrent(alert);
            
    }  
    
//GEN-LINE:MVDMethods

    /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
     */
    private void initialize() {//GEN-END:MVDInitBegin
        // Insert pre-init code here
        
                        
                        
                            try
                            {
                                initPlayer();
                                player.realize();     
                                
                                videoControl = (VideoControl)player.getControl("VideoControl");

                                Canvas canvas = new VideoCanvas(this);
                                canvas.addCommand(stopCapture);
                                canvas.setCommandListener(this);
                                player.start();                                                                                                    
                                getDisplay().setCurrent(canvas);   
                                
                            }
                            catch(Exception e)
                            {
                                showException("Error", new Exception("Cannot Capture"), null);
                            }
                        
//GEN-LINE:MVDInitInit
        // Insert post-init code here
    }//GEN-LINE:MVDInitEnd
    
    
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay() {//GEN-FIRST:MVDGetDisplay
        return Display.getDisplay(this);
    }//GEN-LAST:MVDGetDisplay
    
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet() {//GEN-FIRST:MVDExitMidlet
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }//GEN-LAST:MVDExitMidlet
    
    
    
    /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.//GEN-BEGIN:MVDGetBegin5
     * @return Instance for exitCommand component
     */
    public Command get_exitCommand() {
        if (exitCommand == null) {//GEN-END:MVDGetBegin5
            // Insert pre-init code here
            exitCommand = new Command("Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit5
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd5
        return exitCommand;
    }//GEN-END:MVDGetEnd5
    
    public void startApp() {
        initialize();
    }
    
    
    public void commandAction(Command command, Displayable displayable)
    {
        
        if(command == stopCapture)
        {
           try
                {
                    int width = 200;
                    int height = 150;
                    byte[] pngImage = null;
                     
                    pngImage = videoControl.getSnapshot("encoding=png" + "&width=" + width + "&height=" + height);
                    
                    baos.write(pngImage, 0, pngImage.length);
                    
                    
                }
                catch(Exception  e)
                {
                    showException ("ERROR\n\nCan't capture " , e, menuScreen);
                }
            new UploadJob(this);
        }        
        
        
    }
    
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
}
